--
-- Edicion y carga de nuevos certificados
--


CREATE OR REPLACE FUNCTION public.tg_urls_wsfe()
    RETURNS trigger
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE NOT LEAKPROOF
AS $BODY$
DECLARE 
BEGIN
  IF NEW.nombreservicio = 'wsfe' THEN
     IF (position('HABA' in NEW.COD) > 0 or
         position('HOMO' in NEW.COD) > 0) THEN
		 NEW.URL 	:= 'https://wswhomo.afip.gov.ar/wsfev1/service.asmx';
	 	 NEW.urlaa  := 'https://wsaahomo.afip.gov.ar/ws/services/LoginCms';
	 ELSE
	     NEW.URL 	:= 'https://servicios1.afip.gov.ar/wsfev1/service.asmx';
  	     NEW.urlaa  := 'https://wsaa.afip.gov.ar/ws/services/LoginCms';
	 END IF;
	 NEW.service := 'Service';
	 NEW.port	 := 'ServiceSoap12';
	 NEW.serviceaa	:= 'LoginCMSService';
	 NEW.portaa		:= 'LoginCMS';
  ELSE
     IF (position('HABA' in NEW.COD) > 0 or
         position('HOMO' in NEW.COD) > 0) THEN
		 NEW.URL := 'http://cot.test.arba.gov.ar/TransporteBienes/SeguridadCliente/presentarRemitos.do';
	 ELSE
	     NEW.URL := 'https://cot.arba.gov.ar/TransporteBienes/SeguridadCliente/presentarRemitos.do';
	 END IF;
	 NEW.service 	:= '-';
	 NEW.port	 	:= '-';
	 NEW.urlaa   	:= null;
	 NEW.serviceaa	:= null;
	 NEW.portaa		:= null;
  END IF;
  RETURN NEW;
END;
$BODY$;

ALTER FUNCTION public.tg_urls_wsfe()
    OWNER TO abacouser;


CREATE TRIGGER websrvxemp_ins
    BEFORE INSERT 
    ON public.websrvxemp
    FOR EACH ROW
    EXECUTE FUNCTION public.tg_urls_wsfe();
 
SELECT setval('public.seq_websrvxemp', (SELECT MAX(ID) FROM public.websrvxemp), TRUE);

ALTER TABLE public.websrvxemp
    ALTER COLUMN ID set DEFAULT NEXTVAL('public.seq_websrvxemp');

ALTER TABLE public.websrvxemp
    ALTER COLUMN nombreservicio TYPE character varying(100);

ALTER TABLE public.websrvxemp
    ALTER COLUMN certificado TYPE character varying(2000);

ALTER TABLE public.websrvxemp
    ALTER COLUMN claveprivada TYPE character varying(2000);

UPDATE public.websrvxemp SET habilitado = 'N' WHERE cod ILIKE '%HOMO%' OR cod ILIKE '%HABA%';


--
SELECT aaVersion('524.00','523.00');
--

